package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.RegisterMessage;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;

public class Register2NextActivity extends BaseActivity_NoBar {
	private EditText auth_no;
	private EditText pwd;
	private int times = MainConstants.TIMES_45;
	protected static final int please_auth_code_number = 0;
	protected static final int please_auth_code_wenzi = 1;
	Handler message_handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case please_auth_code_number:
				getAuthNOAgain.setText(times+"秒后重新收取");
				break;

			case please_auth_code_wenzi:
				getAuthNOAgain.setText("请求验证码");
				getAuthNOAgain.setClickable(true);
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	private Button getAuthNOAgain;
	private DataLoader dataLoader;
	private Handler handlerGetAuthNoAgain = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				finishProgress();
				String result = (String) msg.obj;
				if (!result.equals("网络连接错误")) {
					//{"status":false,"errorMsg":"用户已注册"}
					//{"message":{"isExist":false,"randomCode":"428593"},"status":true}
					com.alibaba.fastjson.JSONObject jsonObjectResult= JSON.parseObject(result);
					if("true".equals(jsonObjectResult.getString("status"))){
						RegisterMessage message = jsonObjectResult.getObject("message", RegisterMessage.class);
						intent.putExtra(Register2Activity.RandomCode,message.getRandomCode());
					}
				} else {
					showToast("网络错误!");
				}
				break;
			case HttpTask.RESULT_ERROR:
				finishProgress();
				showToast("网络错误!");
				break;
			}
		}
	};
	private Intent intent;
	public static  String PWD = "PWD";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register2next);
		intent = getIntent();
		getAuthNOAgain = (Button)findViewById(R.id.getAuthNOAgain);
		auth_no = (EditText)findViewById(R.id.auth_no);
		pwd = (EditText)findViewById(R.id.pwd);
		changeAuthNOBtnState();
		dataLoader = new DataLoader(this);
	}
	public void back(View v){
		finish();
	}
	public void getAuthNOAgain(View v){
		changeAuthNOBtnState();
		String url = MainConstants.HOST_test+HttpConfigAPI.NEW_REGISTER;
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("mobileNumber",intent.getStringExtra(Register2Activity.PhoneNumber)));
		new DataLoader(this).DisplayData(url , params , handlerGetAuthNoAgain , "", false);
		showProgress("请稍等...");
	}
	private void changeAuthNOBtnState() {
		getAuthNOAgain.setClickable(false);
		new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(times > 0){
					times --;
					message_handler.sendEmptyMessage(please_auth_code_number);
					
				}else {
					message_handler.sendEmptyMessage(please_auth_code_wenzi);
					times = MainConstants.TIMES_45 ;
					this.cancel();
				}
			}
		}, 1000, 1000);
	}
	public void confirmPwd(View v){
		if(auth_no.getText().toString().trim().equals("")){
			showToast("请输入验证码");
		}else if( ! intent.getStringExtra(Register2Activity.RandomCode).equals(auth_no.getText().toString().trim())){
			showToast("验证码错误");
		}else if(pwd.getText().toString().trim().equals("")){
			showToast("请输入密码");
		}else if(pwd.getText().toString().trim().length() != 6){
			showToast("请输入六位密码");
		}else{
			//mobileNumber,userPassword
			/*String url = MainConstants.HOST_test+HttpConfigAPI.NEW_REGISTER_NEXT;
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("mobileNumber",Register2Activity.PhoneNumber));
			params.add(new BasicNameValuePair("userPassword",pwd.getText().toString().trim()));
			params.add(new BasicNameValuePair("userPassword",pwd.getText().toString().trim()));
			dataLoader.DisplayData(url, params, handler, id, isc);*/
			Intent startIntent = new Intent(this,Register2LastActivity.class);
			startIntent.putExtras(intent);
			startIntent.putExtra(PWD , pwd.getText().toString().trim());
			startActivity(startIntent);
			startActivity_anim(0);
		}
	}
}
